package com.toncen.samepms.dwr;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContextFactory;

import com.toncen.samepms.basic.domain.User;
import com.toncen.samepms.common.ComitConstant;

/**
 * 
 * Title: XXXX
 * <p>
 * Description: ��������ϵͳ��Ϣ���
 * <p>
 * Copyright: Copyright (c) 2012
 * <p>
 * Company:ͨ�׿Ƽ����޹�˾
 * <p>
 * 2012 Feb 29, 2012 5:31:34 PM
 * 
 * @author zq
 * @version 1.0
 */
public class MsgManager {

	/** ���浱ǰ�����û�scriptsession���� */
	public static Map<String, ScriptSession> scriptSessionMap = new HashMap<String, ScriptSession>();

	// �û��ʺ�
	public static final String DWR_USER_ID = "DWR_USER_ID";

	protected Log logger = LogFactory.getLog(this.getClass());

	/**
	 * ���������û��б�
	 * 
	 * @param username
	 *            ����ӵ��б���û���
	 * @param flag
	 *            ������û����б�,����ֻ��õ�ǰ�б�
	 * @param request
	 * @return �û�userid
	 */
	public String updateUsersList(String username, HttpServletRequest request) {
		// ���û�id��ҳ��ű�session��
		this.setScriptSessionFlag(username);
		return "";
	}

	/**
	 * 
	 * TODO ���û�id��ҳ��ű�session��
	 * 
	 * @author Feb 29, 2012 5:43:33 PM ZQ
	 * @param userid
	 * 
	 */
	public void setScriptSessionFlag(String userid) {
		HttpSession session = WebContextFactory.get().getSession();
		// ��½�û���Ϣ
		User user = (User) session.getAttribute(ComitConstant.USER_SESSION_ATTRIBUTE);
		userid = String.valueOf(user.getId());
		ScriptSession ss = WebContextFactory.get().getScriptSession();
		ss.setAttribute(DWR_USER_ID, userid);
		// httpsessionId��scriptsession��
		scriptSessionMap.put(session.getId(), ss);
	}

	/**
	 * 
	 * TODO ����û�id���ָ���û���ҳ��ű�session
	 * 
	 * @author Feb 29, 2012 5:43:45 PM ZQ
	 * @param userid
	 * @param request
	 * @return
	 * 
	 */
	public ScriptSession getScriptSession(String userid, HttpServletRequest request) {
		ScriptSession scriptSessions = null;

		Iterator<Entry<String, ScriptSession>> it = scriptSessionMap.entrySet().iterator();
		Map.Entry<String, ScriptSession> tempMap = null;
		String xuserid = null;
		while (it.hasNext()) {
			tempMap = it.next();
			xuserid = String.valueOf(tempMap.getValue().getAttribute(DWR_USER_ID));
			if (xuserid != null && xuserid.equals(userid)) {
				scriptSessions = tempMap.getValue();
				// util.setStyle("showMessage", "display", "");
				// util.setValue("sender", sender);
				// util.setValue("msg", msg);
				// ���ö�ȡ��Ϣ����
				// util.addFunctionCall("getSystemMessage");
				scriptSessions.addScript(new ScriptBuffer().appendScript("getSystemMessage();"));
			}
		}

		return scriptSessions;
	}

	/**
	 * ������Ϣ
	 * 
	 * @param sender
	 *            ������
	 * @param receiverid
	 *            ������id
	 * @param msg
	 *            ��Ϣ����
	 * @param request
	 */
	public String sendMessage(String sender, String receiverid, String msg, HttpServletRequest request) {
		try {
			// ����û�
			String[] receivers = receiverid.split(",",-1);
			for (String userName : receivers) {
				if (!userName.equals("")) {
					getScriptSession(userName, request);
				}
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return "";
	}
}
